<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级" prop="studentClass">
        <el-input
          v-model="queryParams.studentClass"
          placeholder="请输入班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生生日" prop="studentBithday">
        <el-date-picker
          clearable
          v-model="queryParams.studentBithday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择学生生日"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="学生政治面貌" prop="studentOutlook">
        <el-input
          v-model="queryParams.studentOutlook"
          placeholder="请输入学生政治面貌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生院系" prop="studentFaculties">
        <el-input
          v-model="queryParams.studentFaculties"
          placeholder="请输入学生院系"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生专业" prop="studentSpecialized">
        <el-input
          v-model="queryParams.studentSpecialized"
          placeholder="请输入学生专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省" prop="studentProvince">
        <el-input
          v-model="queryParams.studentProvince"
          placeholder="请输入省"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="studentCity">
        <el-input
          v-model="queryParams.studentCity"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区" prop="studentDistrict">
        <el-input
          v-model="queryParams.studentDistrict"
          placeholder="请输入区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="街道" prop="studentStreet">
        <el-input
          v-model="queryParams.studentStreet"
          placeholder="请输入街道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生年级" prop="studentGrade">
        <el-input
          v-model="queryParams.studentGrade"
          placeholder="请输入学生年级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生民族" prop="studentNation">
        <el-input
          v-model="queryParams.studentNation"
          placeholder="请输入学生民族"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['student:student:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['student:student:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['student:student:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['student:student:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="studentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生ID" align="center" prop="studentId" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="班级" align="center" prop="studentClass" />
      <el-table-column
        label="学生生日"
        align="center"
        prop="studentBithday"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.studentBithday, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="学生政治面貌"
        align="center"
        prop="studentOutlook"
      />
      <el-table-column
        label="学生院系"
        align="center"
        prop="studentFaculties"
      />
      <el-table-column
        label="学生专业"
        align="center"
        prop="studentSpecialized"
      />
      <el-table-column label="省" align="center" prop="studentProvince" />
      <el-table-column label="市" align="center" prop="studentCity" />
      <el-table-column label="区" align="center" prop="studentDistrict" />
      <el-table-column label="街道" align="center" prop="studentStreet" />
      <el-table-column label="用户编号" align="center" prop="uid" />
      <el-table-column label="学生年级" align="center" prop="studentGrade" />
      <el-table-column label="学生民族" align="center" prop="studentNation" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['student:student:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['student:student:remove']"
            >删除</el-button
          >
          <!--             v-hasPermi="['student:student:']"  -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleDiction(scope.row)"
            >查看档案</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="班级" prop="studentClass">
          <el-input v-model="form.studentClass" placeholder="请输入班级" />
        </el-form-item>
        <el-form-item label="学生生日" prop="studentBithday">
          <el-date-picker
            clearable
            v-model="form.studentBithday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择学生生日"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学生政治面貌" prop="studentOutlook">
          <el-input
            v-model="form.studentOutlook"
            placeholder="请输入学生政治面貌"
          />
        </el-form-item>
        <el-form-item label="学生院系" prop="studentFaculties">
          <el-input
            v-model="form.studentFaculties"
            placeholder="请输入学生院系"
          />
        </el-form-item>
        <el-form-item label="学生专业" prop="studentSpecialized">
          <el-input
            v-model="form.studentSpecialized"
            placeholder="请输入学生专业"
          />
        </el-form-item>
        <el-form-item label="省" prop="studentProvince">
          <el-input v-model="form.studentProvince" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="studentCity">
          <el-input v-model="form.studentCity" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区" prop="studentDistrict">
          <el-input v-model="form.studentDistrict" placeholder="请输入区" />
        </el-form-item>
        <el-form-item label="街道" prop="studentStreet">
          <el-input v-model="form.studentStreet" placeholder="请输入街道" />
        </el-form-item>
        <el-form-item label="用户编号" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="学生年级" prop="studentGrade">
          <el-input v-model="form.studentGrade" placeholder="请输入学生年级" />
        </el-form-item>
        <el-form-item label="学生民族" prop="studentNation">
          <el-input v-model="form.studentNation" placeholder="请输入学生民族" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStudent,
  getStudent,
  delStudent,
  addStudent,
  updateStudent,
} from "@/api/student/student";
import { mapGetters } from "vuex";
export default {
  name: "Student",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生信息表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        studentClass: null,
        studentBithday: null,
        studentOutlook: null,
        studentFaculties: null,
        studentSpecialized: null,
        studentProvince: null,
        studentCity: null,
        studentDistrict: null,
        studentStreet: null,
        uid: null,
        studentGrade: null,
        studentNation: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentName: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" },
        ],
        studentClass: [
          { required: true, message: "班级不能为空", trigger: "blur" },
        ],
        studentBithday: [
          { required: true, message: "学生生日不能为空", trigger: "blur" },
        ],
        studentOutlook: [
          { required: true, message: "学生政治面貌不能为空", trigger: "blur" },
        ],
        studentFaculties: [
          { required: true, message: "学生院系不能为空", trigger: "blur" },
        ],
        studentSpecialized: [
          { required: true, message: "学生专业不能为空", trigger: "blur" },
        ],
        studentProvince: [
          { required: true, message: "省不能为空", trigger: "blur" },
        ],
        studentCity: [
          { required: true, message: "市不能为空", trigger: "blur" },
        ],
        studentDistrict: [
          { required: true, message: "区不能为空", trigger: "blur" },
        ],
        studentStreet: [
          { required: true, message: "街道不能为空", trigger: "blur" },
        ],
        uid: [{ required: false, message: "编号不能为空", trigger: "blur" }],
        studentGrade: [
          { required: true, message: "学生年级不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters(["id"]),
  },
  methods: {
    /** 查询学生信息列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams, this.id).then((response) => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        studentId: null,
        studentName: null,
        studentClass: null,
        studentBithday: null,
        studentOutlook: null,
        studentFaculties: null,
        studentSpecialized: null,
        studentProvince: null,
        studentCity: null,
        studentDistrict: null,
        studentStreet: null,
        uid: null,
        studentGrade: null,
        studentNation: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.studentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const studentId = row.studentId || this.ids;
      getStudent(studentId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生信息";
      });
    },
    handleDiction(row) {
      console.log("row", row.studentId);
      this.$router.push({
        path: "/student/personal",
        query: {
          studentId: row.studentId,
        },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.studentId != null) {
            updateStudent(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const studentIds = row.studentId || this.ids;
      this.$modal
        .confirm('是否确认删除学生信息编号为"' + studentIds + '"的数据项？')
        .then(function () {
          return delStudent(studentIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "student/student/export",
        {
          ...this.queryParams,
        },
        `student_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
